package com.study.android.app.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.study.android.app.R;
import com.today.step.lib.Logger;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * Created by shangsong on 17-11-13.
 */

public class TestAppWidgetProvider extends AppWidgetProvider {
    public static final String INTENT_ACTION_APPWIDGET_UPDATE = "com.study.appwidget.UPDATE";
    public static final int BTN_ACTION_SHOW_TOAST = 1;
    private final String TAG = this.getClass().getSimpleName();
    private static Set<Integer> idsSet = new HashSet<>();

    // 图片数组
    private static final int[] ARR_IMAGES = {
            R.mipmap.sample_0,
            R.mipmap.sample_1,
            R.mipmap.sample_2,
            R.mipmap.sample_3,
            R.mipmap.sample_4,
            R.mipmap.sample_5,
            R.mipmap.sample_6,
            R.mipmap.sample_7,
    };

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "onReceive");
        super.onReceive(context, intent);
        String action = intent.getAction();
        Log.i(TAG, "onReceive action=" + action);
        if (TextUtils.equals(INTENT_ACTION_APPWIDGET_UPDATE, action)) {
            updateAllAppWidgets(context);
        } else if (intent.hasCategory(Intent.CATEGORY_ALTERNATIVE)) {
            Uri uri = intent.getData();
            int actionType = Integer.parseInt(uri.getSchemeSpecificPart());
            switch (actionType) {
                case BTN_ACTION_SHOW_TOAST:
                    showToast(context, "Test click appwidget button,show toast!");
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Log.i(TAG, "onUpdate,appWidgetIds.length=" + appWidgetIds.length);
        RegularlySendBroadcastService.startRegularlySendBroadcast(context);
        for (int index = 0; index < appWidgetIds.length; index++) {
            idsSet.add(appWidgetIds[index]);
        }
        ptrSet();
    }

    @Override
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
        Log.i(TAG, "onAppWidgetOptionsChanged");
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        Log.i(TAG, "onDeleted");
        for (Integer id : idsSet) {
            deleteWidgetId(id);
        }
        ptrSet();
    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Log.i(TAG, "onEnabled");
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        Log.i(TAG, "onDisabled");
        RegularlySendBroadcastService.stopRegularlySendBroadcast(context);
    }

    @Override
    public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
        super.onRestored(context, oldWidgetIds, newWidgetIds);
        Log.i(TAG, "onRestored");
    }

    private void ptrSet() {
        Logger.i(TAG, "ptrSet set size=" + idsSet.size());
        for (Integer id : idsSet) {
            printLog(id);
        }
    }

    private void printLog(Integer integer) {
        Logger.i(TAG, "printLog integer=" + integer);
    }

    private void deleteWidgetId(int widgetId) {
        idsSet.remove(widgetId);
    }

    private void updateAllAppWidgets(Context context) {
        Logger.i(TAG, "updateAllAppWidgets,idsSet.size=" + idsSet.size());
        for (Integer id : idsSet) {
            int randomImageIndex = new Random().nextInt(ARR_IMAGES.length);
            Logger.i(TAG, "randomImageIndex=" + randomImageIndex);
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.test_appwidget);
            remoteViews.setImageViewResource(R.id.widget_iv, ARR_IMAGES[randomImageIndex]);
            remoteViews.setOnClickPendingIntent(R.id.widget_btn, getPendingIntent(context, BTN_ACTION_SHOW_TOAST));
            AppWidgetManager.getInstance(context).updateAppWidget(id, remoteViews);
        }
    }

    private PendingIntent getPendingIntent(Context context, int actionType) {
        Logger.e(TAG, "getPendingIntent");
        Intent intent = new Intent();
        intent.setClass(context, TestAppWidgetProvider.class);
        intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
        intent.setData(Uri.parse("custom:" + actionType));
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }

    private void showToast(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
}
